﻿var obj = {
    productId: '',
    productNo: '',
    productName: "",
    manuNo: '',
    insideNo: '',
    unitId: '',
    productTypeId: '',
    goOnline: '',
    online: '',
    productSpec: '',
    productPrice: 0,
    warehouseId: 0,
    binLocationId: '',
    vendorId: '',
    clientId: '',
    productDesc: '',
    productWrap: '',
    productWeight: '',
}

var index = 1;
var size = 100;
var tacount = 0;
$(function () {
    Show()

});
function Show() {
    layui.use('table', function () {
        var table = layui.table;

        var productNo = $("#productNoss").val();
        var productName = $("#productNames").val();
        var manuNo = $("#manuNos").val();
        var vendorId = $("#vendorIds").val();
        $.ajax({
            url: '../Product/GEtProductShow',
            async: false,//改为异步,
            type: 'get',
            data: {
                productNo: productNo,
                productName: productName,
                manuNo: manuNo,
                vendorId: vendorId,
                index: index,
                size, size,
                token: localStorage.getItem("token")
            },
            success: function (data) {

                res = JSON.parse(data);
                console.log(res)
                if (res.code === 0) {
                    tacount = res.data.tacount
                    var inst = table.render({
                        elem: productShow,
                        data: res.data.list,
                        toolbar: '#toolbarDemo',
                        defaultToolbar: ['exports'],
                        cols: [[
                            { type: 'checkbox', fixed: 'left' },
                            { field: 'productNo', title: '产品编号' },
                            { field: 'productName', title: '产品名称' },
                            { field: 'manuNo', title: '厂商编码' },
                            { field: 'insideNo', title: '内部编码' },
                            { field: 'productSpec', title: '规格' },
                            { field: 'productTypeName', title: '类别' },
                            { field: 'productSpec', title: '存储单位' },
                            { field: 'online', title: '预警（下）' },
                            { field: 'goOnline', title: '预警（上）' },
                            { field: 'productWrap', title: '包装类型' },
                            { field: 'productPrice', title: '价格' },
                            { field: 'productWeight', title: '重量' },
                            { field: 'clientId', title: '默认供应商' },
                            {
                                field: '', title: '操作', templet: function (d) {
                                    return '<a class="layui-btn layui-btn-edit layui-btn-xs"  onclick="editData(' + d.productId + ')"><i class="layui-icon layui-icon-edit"></i>编辑</a> ' +
                                        '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="productDel(' + d.productId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                                }
                            }
                        ]],
                        page: true, // 是否显示分页
                        count: tacount,
                        limits: [15, 20, 30],
                        limit: 15,// 每页默认显示的数量
                        curr: index,


                    });

                } else {
                    console.log(data.msg)
                }

            },
        })

    })

}


function refesh() {
    location.reload();
}


//弹窗添加

layui.use(function () {
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form;
    // 事件
    util.on('lay-on', {
        'test-page-custom': function () {
            layer.open({
                type: 1,
                area: '800px',
                resize: false,
                shadeClose: true,
                title: '新增供应商',
                content: `<form class="layui-form layui-form-pane" action="" style="text-align: center;width:100%">

                        <div class="layui-form-item">
                            <label class="layui-form-label">产品编号：</label>
                            <div class="layui-input-inline">
                                            <input type="text" id="productNos" name="productNo"  placeholder="可不填写,自动生成" autocomplete="off" class="layui-input">
                            </div>
                                <label class="layui-form-label">产品名称：</label>
                                <div class="layui-input-inline">
                                                        <input type="text"  lay-verify="required" lay-reqtext="请填产品名称" id="productName" name="productName" autocomplete="off" class="layui-input">
                                </div>
                              </div>

                                  <div class="layui-form-item">
                                <label class="layui-form-label">厂家编码：</label>
                                <div class="layui-input-inline">
                                                        <input type="text"  lay-verify="required" lay-reqtext="请填厂家编码"  id="manuNo" name="manuNo" autocomplete="off" class="layui-input">
                                </div>
                                    <label class="layui-form-label">内部编码：</label>
                                    <div class="layui-input-inline">
                                                            <input type="text"  lay-verify="required" lay-reqtext="请填内部编码"  id="insideNo" name="insideNo" autocomplete="off" class="layui-input">
                                    </div>
                                  </div>
                                              <div style="width:350px" class="layui-form layui-row layui-col-space16">
                                       <label class="layui-form-label">单位：</label>
                                                   <div class="layui-col-md6">
                                                                            <select id="unitId" name="unitId"  style="height:initial"  lay-verify="required" lay-append-to="body" lay-append-position="fixed">
                                                  <option value="">请选择</option>
                                                      <option value="3">个</option>
                                                      <option value="4">平方米</option>
                                                          <option value="5">立方米</option>
                                                </select>
                                              </div>
                                             <label class="layui-form-label">类别：</label>
                                                       <div class="layui-col-md6">
                                                                    <select id="productTypeId" name="productTypeId"  lay-verify="required" lay-append-to="body" lay-append-position="fixed">
                                                      <option value="">请选择</option>
                                                          <option value="1">智能设备</option>
                                                          <option value="2">高值耗材</option>
                                                          <option value="3">低值易耗品</option>
                                                    </select>
                                                  </div>
                                               </div>
                                            <div class="layui-form-item">
                                    <label class="layui-form-label">上线值：</label>
                                    <div class="layui-input-inline">
                                                    <input type="text"  lay-verify="required" id="goOnline" name="goOnline" autocomplete="off" class="layui-input">
                                    </div>
                                        <label class="layui-form-label">下限值：</label>
                                        <div class="layui-input-inline">
                                                        <input type="text"  lay-verify="required" id="online" name="online" autocomplete="off" class="layui-input">
                                        </div>
                                      </div>
                                          <div class="layui-form-item">
                                        <label class="layui-form-label">规格：</label>
                                        <div class="layui-input-inline">
                                                        <input type="text"  lay-verify="required" id="productSpec" name="productSpec" autocomplete="off" class="layui-input">
                                        </div>
                                            <label class="layui-form-label">价格：</label>
                                            <div class="layui-input-inline">
                                                            <input type="text"  lay-verify="required" id="productPrice" name="productPrice" autocomplete="off" class="layui-input">
                                            </div>
                                          </div>
                                                       <div style="width:250px" class="layui-form layui-row layui-col-space16">
                                           <label class="layui-form-label">默认仓库：</label>
                                                        <div class="layui-col-md6">
                                                                <select id="warehouseId" name="warehouseId" lay-append-to="body" lay-append-position="fixed">
                                                      <option value="">请选择</option>
                                                      <option value="1">1号仓库</option>
                                                      <option value="2">2号仓库</option>
                                                      <option value="3">3号仓库</option>
                                                    </select>
                                                  </div>

                                              <label class="layui-form-label">默认库位：</label>

                                                     <div class="layui-col-md6">
                                                                <select id="binLocationId" name="binLocationId" lay-append-to="body" lay-append-position="fixed">
                                                      <option value="">请选择</option>
                                                      <option value="1">正式库区</option>
                                                      <option value="2">待入库区</option>
                                                      <option value="3">待检库区</option>
                                                          <option value="4">待出库区</option>
                                                    </select>
                                                  </div>

                                                   </div>
                                                        <div class="layui-form-item">
                    <label class="layui-form-label">默认供应商：</label>
                    <div class="layui-input-inline">
                                <input type="text" id="vendorId" name="vendorId" autocomplete="off" class="layui-input">
                    </div>
                        <label class="layui-form-label">默认客户：</label>
                        <div class="layui-input-inline">
                                    <input type="text" id="clientId" name="clientId" autocomplete="off" class="layui-input">
                        </div>
                      </div>
                                    <div style="width:450px" class="layui-form-item">
                                    <label class="layui-form-label">描述：</label>
                                        <div  style="width:450px" class="layui-input-inline">
                                                  <textarea id="productDesc" name="productDesc" class="layui-textarea"></textarea>
                                    </div>
                                </div>

                                    <div class="layui-form-item">
                                    <button class="layui-btn" lay-submit lay-filter="demo-login">提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">取消</button>
                    </div>
                    </form>`,
                success: function () {
                    // 对弹层中的表单进行初始化渲染
                    form.render();
                    // 表单提交事件
                    form.on('submit(demo-login)', function (data) {
                        var field = data.field; // 获取表单字段值
                        // 显示填写结果，仅作演示用

                        // 此处可执行 Ajax 等操作
                        // …
                        $.ajax({
                            url: '../Product/GEtProductAdd',
                            type: 'post',
                            //contentType: 'application/json',
                            data: {
                                data: JSON.stringify(field),
                            },

                            success: function (res) {
                                console.log(field)
                                console.log(res)
                                var s = JSON.parse(res)
                                if (s != "") {
                                    layer.alert("添加成功");
                                    location.href = "/Product/ShowIndex"
                                }
                                else {
                                    layer.alert("添加失败");
                                }
                            }
                        })
                        return false; // 阻止默认 form 跳转
                    });
                }
            });
            var n = new Date();
            var ss = n.getFullYear() + (n.getMonth() + 1).toString().padStart(2, '0') +
                n.getDate().toString().padStart(2, '0') +
                n.getHours().toString().padStart(2, '0') +
                n.getMinutes().toString().padStart(2, '0') +
                n.getSeconds().toString().padStart(2, '0');
            $("#productNos").val(ss);
        }
    })



    //反填
    window.editData = function (productId) {
        $.ajax({
            url: '../Product/GetProductFind',
            type: 'Get',
            data: { productId: productId, token: localStorage.getItem("token") },

            success: function (res) {
                // var jsonString = JSON.stringify(res)
                var parsedData = JSON.parse(res);
                console.log(parsedData)
                $(parsedData.data).each(function () {
                    console.log(parsedData.data)
                    console.log(this.productNo)
                    $("#productNo").val(this.productNo);
                    $("#productName").val(this.productName);
                    $("#manuNo").val(this.manuNo);
                    $("#insideNo").val(this.insideNo);
                    $("#unitId").val(this.unitId);
                    $("#productTypeId").val(this.productTypeId);
                    $("#goOnline").val(this.goOnline);
                    $("#online").val(this.online);
                    $("#productSpec").val(this.productSpec);
                    $("#productPrice").val(this.productPrice);
                    $("#warehouseId").val(this.warehouseId);
                    $("#binLocationId").val(this.binLocationId);
                    $("#vendorId").val(this.vendorId);
                    $("#clientId").val(this.clientId);
                    $("#productDesc").val(this.productDesc);
                    $("#productWrap").val(this.productWrap)
                    $("#productWeight").val(this.ProductWeight)


                    obj.productId = this.productId
                    console.log(obj.productId)
                    obj.productNo = this.productNo
                    obj.productName = this.productName
                    obj.manuNo = this.manuNo
                    obj.insideNo = this.insideNo
                    obj.unitId = this.unitId
                    obj.productTypeId = this.productTypeId
                    obj.goOnline = this.goOnline
                    obj.online = this.online
                    obj.productSpec = this.productSpec
                    obj.productPrice = this.productPrice
                    obj.warehouseId = this.warehouseId
                    obj.binLocationId = this.binLocationId
                    obj.vendorId = this.vendorId
                    obj.clientId = this.clientId
                    obj.productDesc = this.productDesc
                    obj.productWrap = this.productWrap
                    obj.productWeight = this.productWeight

                })
            },
        })

        layer.open({
            type: 1,
            title: '新增供应商',
            content: `<form class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">产品编号：</label>
                    <div class="layui-input-inline">
                                    <input type="text" id="productNo" name="productNo" placeholder="可不填写,自动生成" autocomplete="off" class="layui-input"/>
                    </div>
                        <label class="layui-form-label">产品名称：</label>
                        <div class="layui-input-inline">
                                        <input type="text" id="productName" name="productName" autocomplete="off" class="layui-input"/>
                        </div>
                      </div>

                          <div class="layui-form-item">
                        <label class="layui-form-label">厂家编码：</label>
                        <div class="layui-input-inline">
                                        <input type="text" id="manuNo" name="manuNo" autocomplete="off" class="layui-input"/>
                        </div>
                            <label class="layui-form-label">内部编码：</label>
                            <div class="layui-input-inline">
                                            <input type="text" id="insideNo" name="insideNo" autocomplete="off" class="layui-input"/>
                            </div>
                          </div>
                                      <div style="width:350px" class="layui-form layui-row layui-col-space16">
                               <label class="layui-form-label">单位：</label>
                                           <div class="layui-col-md6">
                                                                <select id="unitId" name="unitId"  style="height:initial" lay-append-to="body" lay-append-position="fixed">
                   
                                              <option value="3">个</option>
                                              <option value="4">平方米</option>
                                                  <option value="5">立方米</option>
                                        </select>
                                      </div>
                                     <label class="layui-form-label">类别：</label>
                                               <div class="layui-col-md6">
                                                        <select id="productTypeId" name="productTypeId" lay-append-to="body" lay-append-position="fixed">
                                                  <option value="1">智能设备</option>
                                                  <option value="2">高值耗材</option>
                                                  <option value="3">低值易耗品</option>
                                            </select>
                                          </div>
                                       </div>
                                    <div class="layui-form-item">
                            <label class="layui-form-label">上线值：</label>
                            <div class="layui-input-inline">
                                        <input type="text" id="goOnline" name="goOnline" autocomplete="off" class="layui-input">
                            </div>
                                <label class="layui-form-label">下限值：</label>
                                <div class="layui-input-inline">
                                            <input type="text" id="online" name="online" autocomplete="off" class="layui-input">
                                </div>
                              </div>
                                  <div class="layui-form-item">
                                <label class="layui-form-label">规格：</label>
                                <div class="layui-input-inline">
                                            <input type="text" id="productSpec" name="productSpec" autocomplete="off" class="layui-input">
                                </div>
                                    <label class="layui-form-label">价格：</label>
                                    <div class="layui-input-inline">
                                                <input type="text" id="productPrice" name="productPrice" autocomplete="off" class="layui-input">
                                    </div>
                                  </div>
                                               <div style="width:250px" class="layui-form layui-row layui-col-space16">
                                   <label class="layui-form-label">默认仓库：</label>
                                                <div class="layui-col-md6">
                                                        <select id="warehouseId" name="warehouseId" lay-append-to="body" lay-append-position="fixed">
                                              <option value="1">1号仓库</option>
                                              <option value="2">2号仓库</option>
                                              <option value="3">3号仓库</option>
                                            </select>
                                          </div>

                                      <label class="layui-form-label">默认库位：</label>

                                             <div class="layui-col-md6">
                                                        <select id="binLocationId" name="binLocationId" lay-append-to="body" lay-append-position="fixed">
                                              <option value="1">正式库区</option>
                                              <option value="2">待入库区</option>
                                              <option value="3">待检库区</option>
                                                  <option value="4">待出库区</option>
                                            </select>
                                          </div>

                                           </div>
                                                <div class="layui-form-item">
            <label class="layui-form-label">默认供应商：</label>
            <div class="layui-input-inline">
                        <input type="text" id="vendorId" name="vendorId" autocomplete="off" class="layui-input">
            </div>
                <label class="layui-form-label">默认客户：</label>
                <div class="layui-input-inline">
                            <input type="text" id="clientId" name="clientId" autocomplete="off" class="layui-input">
                </div>
              </div>
                            <div style="width:450px" class="layui-form-item">
                            <label class="layui-form-label">描述：</label>
                                <div  style="width:450px" class="layui-input-inline">
                                  <textarea id="productDesc" name="productDesc" class="layui-textarea"></textarea>
                            </div>
                        </div>

                            <div class="layui-form-item">
                            <button class="layui-btn" lay-submit lay-filter="demo-login">提交<button>
                <button type="reset" class="layui-btn layui-btn-primary">取消<button>
            </div>
            </form>`,

            area: '800px',
            success: function () {
                var obj1 = {
                    productId: '',
                    productNo: '',
                    productName: '',
                    manuNo: '',
                    insideNo: '',
                    unitId: '',
                    productTypeId: '',
                    goOnline: '',
                    online: '',
                    productSpec: '',
                    productPrice: '',
                    warehouseId: '',
                    binLocationId: '',
                    vendorId: '',
                    clientId: '',
                    productDesc: '',
                    productWrap: '',
                    productWeight: '',
                };


                var token = localStorage.getItem("token")
                // 对弹层中的表单进行初始化渲染
                form.render();
                // 表单提交事件
                form.on('submit(demo-login)', function (data) {
                    var field = data.field; // 获取表单字段值
                    // 显示填写结果，仅作演示用
                    obj1.productId = obj.productId
                    obj1.productNo = field.productNo
                    obj1.productName = field.productName
                    obj1.manuNo = field.manuNo
                    obj1.insideNo = field.insideNo
                    obj1.unitId = field.unitId
                    obj1.productTypeId = field.productTypeId
                    obj1.goOnline = field.goOnline
                    obj1.online = field.online
                    obj1.productSpec = field.productSpec
                    obj1.productPrice = field.productPrice
                    obj1.warehouseId = field.warehouseId
                    obj1.binLocationId = field.binLocationId
                    obj1.vendorId = field.vendorId
                    obj1.clientId = field.clientId
                    obj1.productDesc = field.productDesc
                    obj1.productWrap = field.productWrap
                    obj1.productWeight = field.productWeight
                    // 此处可执行 Ajax 等操作
                    // …
                    $.ajax({
                        url: '../Product/GEtProductUpdate',
                        type: 'post',
                        //contentType: 'application/json',
                        data: { obj1, token: token },
                        success: function (res) {
                            console.log(obj1)
                            var list = JSON.parse(res)
                            console.log(list)
                            if (list.code == 1) {
                                layer.alert("修改成功")
                                setTimeout(function () {
                                    location.reload();
                                }, 2000);
                            }
                            else {
                                layer.alert("修改失败")
                            }
                            console.log(res)

                        }
                    })
                    return false; // 阻止默认 form 跳转
                });
            }
        })



    }

});


//产品批量删除
$('#batchDeleteButton').click(function () {
    var table = layui.table;
    // 获取选中行的数据（这里主要是获取warehouseId）
    var checkStatus = table.checkStatus('productShow');
    var selectedData = checkStatus.data.map(function (item) {
        return item.productId;
    });

    if (selectedData.length === 0) {
        layer.msg("请至少选择一条要删除的数据", { icon: 2 });
        return;
    }

    console.log(selectedData);
    layer.confirm('确定要批量删除选中的' + selectedData.length + '条数据吗？', function (index) {
        var productIds = {
            productId: selectedData
        }
        // 发送批量删除请求到后端
        $.ajax({
            url: "../Product/ProductDelRange", // 后端处理批量删除的接口地址，需根据实际情况调整
            type: "post",
            // contentType: "application/json",
            data: {
                productId: selectedData,
                token: localStorage.getItem("token"),
            },
            success: function (res) {
                var res = JSON.parse(res)
                console.log(res);
                if (res.code > 0) {
                    layer.msg("批量删除成功", { icon: 1 })
                    // 重新加载表格数据，展示更新后的列表
                    Show()
                } else {
                    layer.msg("批量删除失败:" + res.message, { icon: 2 });
                }
            },
            error: function () {
                console.log("批量删除数据失败");
                // 可以考虑显示一个错误消息给用户
            }
        });
    })
})

function checkAll() {

    var checked = $("#checkAll").prop("checked");  //取全选/反选 checkbox的选中状态值
    $("input[name='ckbproc']").prop("checked", checked);//把所有name=ckbproc的checkbox的选中状态赋值为 checked
    console.log(checked)
}

//逻辑删除
window.productDel = function (productId) {
    var obj = JSON.stringify({
        "productId": productId
    })

    layer.confirm('确定要删除该条数据吗？', function (index) {

        $.ajax({
            type: 'post',
            url: '../Product/ProductDel',
            //contentType: 'application/json',//浏览器里报415错
            data: { obj: obj, token: localStorage.getItem("token") },
            success: function (res) {
                if (res != "") {
                    layer.msg('删除成功', { icon: 1 });
                    Show()
                    return;
                } else {
                    layer.alert("删除失败!");
                }
            },
            error: function () {
                layer.msg('删除失败，请稍后重试', {
                    time: 2000,
                    icon: 2
                });
            }

        })
        layer.close(index);
    })

}


